//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "BoundaryConstraint.h"
using namespace Cephei::QL::Math::Optimization;
#include <gen/QL/Math/Array.h>
#include <gen/QL/Math/Optimization/Constraint.h>
using namespace Cephei::QL::Math;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Math::Optimization::CBoundaryConstraint::CBoundaryConstraint (Double low, Double high) : CConstraint(CBoundaryConstraint::typeid)
{
    try
    {
#ifdef HANDLE
        _phBoundaryConstraint = NULL;
#endif
        QuantLib::Real _low = (QuantLib::Real)ValueHelper::Convert (low);
        QuantLib::Real _high = (QuantLib::Real)ValueHelper::Convert (high);
        _ppBoundaryConstraint = new boost::shared_ptr<QuantLib::BoundaryConstraint> (new QuantLib::BoundaryConstraint ( _low,  _high ));
        SetConstraint (boost::dynamic_pointer_cast<QuantLib::Constraint> (*_ppBoundaryConstraint));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Math::Optimization::CBoundaryConstraint::CBoundaryConstraint (boost::shared_ptr<QuantLib::BoundaryConstraint>& childNative, Object^ owner) : CConstraint(CBoundaryConstraint::typeid)
{
#ifdef HANDLE
	_phBoundaryConstraint = NULL;
#endif
	_ppBoundaryConstraint = &childNative;
    _ppConstraint = new boost::shared_ptr<QuantLib::Constraint> (boost::dynamic_pointer_cast<QuantLib::Constraint> (*_ppBoundaryConstraint));
}
Cephei::QL::Math::Optimization::CBoundaryConstraint::CBoundaryConstraint (QuantLib::BoundaryConstraint& childNative, Object^ owner) : CConstraint(CBoundaryConstraint::typeid)
{
#ifdef HANDLE
	_phBoundaryConstraint = NULL;
#endif
	_ppBoundaryConstraint = new boost::shared_ptr<QuantLib::BoundaryConstraint> (&childNative);
    _ppConstraint = new boost::shared_ptr<QuantLib::Constraint> (boost::dynamic_pointer_cast<QuantLib::Constraint> (*_ppBoundaryConstraint));
    _BoundaryConstraintOwner = owner;
    _ConstraintOwner = owner;
}

Cephei::QL::Math::Optimization::CBoundaryConstraint::CBoundaryConstraint (CBoundaryConstraint^ copy) : CConstraint(CBoundaryConstraint::typeid)
{
#ifdef HANDLE
	_phBoundaryConstraint = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppBoundaryConstraint = new boost::shared_ptr<QuantLib::BoundaryConstraint> (copy->GetShared());
        _ppConstraint = new boost::shared_ptr<QuantLib::Constraint> (boost::dynamic_pointer_cast<QuantLib::Constraint> (*_ppBoundaryConstraint));
    }
}
Cephei::QL::Math::Optimization::CBoundaryConstraint::CBoundaryConstraint (System::Type^ t) : CConstraint(CBoundaryConstraint::typeid)
{
#ifdef HANDLE
	_phBoundaryConstraint = NULL;
#endif
	if (!t->IsSubclassOf(CBoundaryConstraint::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Math::Optimization::CBoundaryConstraint::CBoundaryConstraint (QuantLib::Handle<QuantLib::BoundaryConstraint>& childNative, Object^ owner)  : CConstraint(CBoundaryConstraint::typeid)
{
	_phBoundaryConstraint = &childNative;
	_ppBoundaryConstraint = &static_cast<boost::shared_ptr<QuantLib::BoundaryConstraint>>(childNative.currentLink());
    _ppConstraint = new boost::shared_ptr<QuantLib::Constraint> (boost::dynamic_pointer_cast<QuantLib::Constraint> (*_ppBoundaryConstraint));
    _BoundaryConstraintOwner = owner;
}
Cephei::QL::Math::Optimization::CBoundaryConstraint::CBoundaryConstraint (QuantLib::Handle<QuantLib::BoundaryConstraint> childNative)  : CConstraint(CBoundaryConstraint::typeid)
{
	_phBoundaryConstraint = &childNative;
	_ppBoundaryConstraint = &static_cast<boost::shared_ptr<QuantLib::BoundaryConstraint>>(childNative.currentLink());
    _ppConstraint = new boost::shared_ptr<QuantLib::Constraint> (boost::dynamic_pointer_cast<QuantLib::Constraint> (*_ppBoundaryConstraint));
}
#endif
#ifdef STRUCT
Cephei::QL::Math::Optimization::CBoundaryConstraint::CBoundaryConstraint (QuantLib::BoundaryConstraint childNative)  : CConstraint(CBoundaryConstraint::typeid)
{
#ifdef HANDLE
	_phBoundaryConstraint = NULL;
#endif
	_ppBoundaryConstraint = new boost::shared_ptr<QuantLib::BoundaryConstraint> (new QuantLib::BoundaryConstraint (childNative));
    _ppConstraint = new boost::shared_ptr<QuantLib::Constraint> (boost::dynamic_pointer_cast<QuantLib::Constraint> (*_ppBoundaryConstraint));
}
#endif

Cephei::QL::Math::Optimization::CBoundaryConstraint::~CBoundaryConstraint ()
{
    if (_ppBoundaryConstraint != NULL)
    {
	    delete _ppBoundaryConstraint;
        _ppBoundaryConstraint = NULL;
    }
}
Cephei::QL::Math::Optimization::CBoundaryConstraint::!CBoundaryConstraint ()
{
    if (_ppBoundaryConstraint != NULL)
    {
	    delete _ppBoundaryConstraint;
    }
}
QuantLib::BoundaryConstraint& Cephei::QL::Math::Optimization::CBoundaryConstraint::GetReference ()
{
    if (_ppBoundaryConstraint == NULL) throw gcnew NativeNullException ();
	return **_ppBoundaryConstraint;
}
boost::shared_ptr<QuantLib::BoundaryConstraint>& Cephei::QL::Math::Optimization::CBoundaryConstraint::GetShared ()
{
    if (_ppBoundaryConstraint == NULL) throw gcnew NativeNullException ();
	return *_ppBoundaryConstraint;
}
QuantLib::BoundaryConstraint* Cephei::QL::Math::Optimization::CBoundaryConstraint::GetPointer ()
{
    if (_ppBoundaryConstraint == NULL) throw gcnew NativeNullException ();
	return &**_ppBoundaryConstraint;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::BoundaryConstraint>& Cephei::QL::Math::Optimization::CBoundaryConstraint::GetHandle ()
{
	if (_phBoundaryConstraint == NULL)
	{
		_phBoundaryConstraint = new Handle<QuantLib::BoundaryConstraint> (*_ppBoundaryConstraint);
	}
	return *_phBoundaryConstraint;
}
#endif
bool Cephei::QL::Math::Optimization::CBoundaryConstraint::HasNative () 
{
	return (_ppBoundaryConstraint != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Math::Optimization::IBoundaryConstraint^ Cephei::QL::Math::Optimization::CBoundaryConstraint_Factory::Create (Double low, Double high)
{
    return gcnew CBoundaryConstraint ( low,  high);
}
